using Prism.Mvvm;

namespace MoniterPLC
{
    /// <summary>
    /// 工位状态枚举
    /// </summary>
    public enum StationStatus
    {
        /// <summary>运行中状态</summary>
        Running = 0,
        /// <summary>异常状态</summary>
        Abnormal = 1,
        /// <summary>待运行状态</summary>
        Waiting = 2
    }

    /// <summary>
    /// 工位模型类，用于表示和管理工位的状态信息
    /// </summary>
    public class StationModel : BindableBase
    {
        private int _id;
        private string? _name;
        private string? _address;
        private StationStatus _status;

        /// <summary>
        /// 工位ID
        /// </summary>
        public int Id
        {
            get { return _id; }
            set { SetProperty(ref _id, value); }
        }

        /// <summary>
        /// 工位名称
        /// </summary>
        public string? Name
        {
            get { return _name; }
            set { SetProperty(ref _name, value); }
        }

        /// <summary>
        /// 工位在PLC中的地址
        /// </summary>
        public string? Address
        {
            get { return _address; }
            set { SetProperty(ref _address, value); }
        }

        /// <summary>
        /// 工位状态
        /// </summary>
        public StationStatus Status
        {
            get { return _status; }
            set 
            { 
                // 使用SetProperty方法，当_status改变时，触发Status属性的变化通知
                if (SetProperty(ref _status, value))
                {
                    // 额外触发StatusText属性的变化通知，确保UI能正确更新状态文本
                    RaisePropertyChanged(nameof(StatusText));
                }
            }
        }
        
        /// <summary>
        /// 状态文本描述
        /// </summary>
        public string StatusText
        {
            get
            {
                return _status switch
                {
                    StationStatus.Running => "运行中",
                    StationStatus.Abnormal => "异常",
                    _ => "待运行",
                };
            }
        }

        /// <summary>
        /// 初始化工位模型的默认构造函数
        /// </summary>
        public StationModel()
        {
            Id = 0;
            Name = string.Empty;
            Address = string.Empty;
            Status = StationStatus.Waiting; // 默认状态为待运行
        }
        
        /// <summary>
        /// 获取或设置工位是否正在运行
        /// </summary>
        public bool IsRunning
        {
            get { return _status == StationStatus.Running; }
            set 
            { 
                // 设置Status属性，它会自动处理属性变更通知
                Status = value ? StationStatus.Running : StationStatus.Waiting;
                // 额外触发StatusText的变更通知，确保UI更新
                RaisePropertyChanged(nameof(StatusText));
            }
        }
    }
}